/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
  <file> fdfilter.h </file>
  <brief>
    fdfilter is a collection of API for manipulating raw input.
    Given input by fd, fdfilter reads from it and oupput to another fd, filtering the stream by 'filter-prog'
    The 'filter-prog' is any executable of which input is from stdin and of output is to stdout.
    This file is part of ULS, Unified Lexical Scheme.
  </brief>
  <author>
    Stanley J. Hong <babel96@gmail.com>, 2011.
  </author>
*/

#ifndef __FDFILTER_H__
#define __FDFILTER_H__

#include "uls_type.h"

#ifdef WIN32
typedef uls_int32 uls_pid_t;
#else
#include <unistd.h>
typedef pid_t uls_pid_t ;
#endif

#ifdef __cplusplus
extern "C" {
#endif

#define FDF_N_PROCS_POPEN  2

typedef int (*fdf_iprovider_t)(int fd, int writefd);

typedef struct _fdf fdf_t;
struct _fdf {
	fdf_iprovider_t i_provider;
	char *filter;

	int fd, fd_org;
	uls_pid_t child_pid[FDF_N_PROCS_POPEN];
};

void fdf_init(fdf_t *fdflt, fdf_iprovider_t i_provider, char* cmdline);
void fdf_reset(fdf_t *fdflt, fdf_iprovider_t i_provider, char* cmdline);
void fdf_deinit(fdf_t *fdflt);
int fdf_open(fdf_t *fdflt, int fd);
int fdf_close(fdf_t* fd_flt);
int fdf_iprovider_simple(int fdin, int writefd);
int fdf_iprovider_filelist(int fd_list, int writefd);

char** uls_pars_cmdline(char* cmdline, char** p_line, int* p_args);
int uls_execv_cmdline(char* cmdline);
int uls_proc_join(uls_pid_t *child_pid, int n_child_pid);

#ifdef __cplusplus
}
#endif

#endif
